package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.UserInfo;
import tech.aistar.entity.dynamic.ImgsStorage;
import tech.aistar.entity.trading.Goods;
import tech.aistar.entity.trading.GoodsDetails;
import tech.aistar.entity.trading.Report;
import tech.aistar.service.IGoodsService;
import tech.aistar.service.IGoodsTypeService;
import tech.aistar.service.IUserInfoService;
import tech.aistar.service.IUserLoginService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static com.sun.xml.internal.ws.api.message.Packet.Status.Request;

/**
 * @author Ace
 * @version 1.0
 * @descriiption:本类用来演示:
 * @date 2019/10/6 0006.
 */
@Controller
@RequestMapping("/goodsDetails")
public class GoodsDetailsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IUserLoginService userLoginService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IGoodsTypeService goodsTypeService;
    @RequestMapping("/list")
    public String list(Model model, String id){
        GoodsDetails goodsDetails=goodsService.findBygoodsId(Integer.valueOf(id));
        model.addAttribute("goodsDetails",goodsDetails);
        Goods goods=goodsService.findById(Integer.valueOf(id));
        Integer goodsTypeId=Integer.valueOf(goods.getGoodsTypeId());
        model.addAttribute("goodsTypeId",goodsTypeId);
        System.out.println("==============="+goodsTypeId);
//      浏览数增加
        if (goods!=null) {
            goods.setBrowse(goods.getBrowse() + 1);
            goodsService.Update(goods);
        }
        model.addAttribute("goods",goods);
        UserInfo userInfo=userInfoService.findById(goods.getUserId());
        model.addAttribute("userInfo",userInfo);

        List<Goods>goods1=goodsService.findAll1();
        model.addAttribute("goods1",goods1);

        List<ImgsStorage> imgsStoragesList=goodsService.fingimgByGoodsId(Integer.valueOf(id));
        model.addAttribute("imgStoragesList",imgsStoragesList);
        return "trading/itemsinfo";
    }
/**
 * 举报
 */
    @PostMapping("/report")
    @ResponseBody
    public String report(String uId,HttpServletRequest req) throws ServletException, IOException{

        if (req.getSession().getAttribute("user")==null){
            return "未登录";
        }else{
            Report report= goodsService.findReportByuId(uId);
            report.setReport(report.getReport()+1);
            goodsService.updateReport(report);
            return "可举报";
        }
    }
    /**
     * 收藏
     */
    @RequestMapping("/collection")
    @ResponseBody
    public String collection(Integer uId,Integer kindId,HttpServletRequest req){

        if (req.getSession().getAttribute("user")==null){
            return "未登录";
        }else{
        goodsService.addGoods(uId,kindId);
        return "可收藏";
    }
    }
}
